package com.bzxhh.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//演示线程池的三种常见分类
public class ThreadPoolDemo01 {

    public static void main(String[] args) {
//        //1池n线程
//        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);
//
//        //10个顾客
//        try {
//            for (int i = 0; i < 10; i++) {
//                //执行
//                threadPool1.execute(() -> {
//                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
//                });
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        } finally {
//            threadPool1.shutdown();
//        }

//        //1池1线程
//        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();
//
//        //10个顾客
//        try {
//            for (int i = 0; i < 10; i++) {
//                //执行
//                threadPool2.execute(() -> {
//                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
//                });
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        } finally {
//            threadPool2.shutdown();
//        }

        //1池可扩容线程
        ExecutorService threadPool3 = Executors.newCachedThreadPool();

        //10个顾客
        try {
            for (int i = 0; i < 10; i++) {
                //执行
                threadPool3.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            threadPool3.shutdown();
        }

    }
}
